<?php
class myAction extends sfAction
{
	public $sess_lang = 'ch';
	
	public function execute($request) {
		$this->__setLang($request);
		$this->__setTitle();
		return sfView::NONE;
	}
	
	private function __setTitle() {
		$response = $this->getResponse();
		$response->addMeta('keywords','EDMIC.cn 技术售后服务站');
		$response->addMeta('description','EDMIC.cn 技术售后服务站');
		$response->setTitle('EDMIC.cn 技术售后服务站');
	}
	
	private function __setLang($request) {
		$sess_lang = $this->getUser()->getAttribute('lang');
		if ($sess_lang != '') {
			$this->sess_lang = $sess_lang;
		} else
			$this->sess_lang = 'ch';		
		if ($this->sess_lang != 'ch') {
			// 切换语言
			$module_name = $this->getModuleName();
			$action_name = $this->getActionName();
			$request_url = parse_url($request->getUri());
			$request_url = $request_url['path'];
			$i = strpos($request_url, $module_name);
			if ($i !== false) {				
				$i = strpos($request_url, $action_name);
				if ($i !== false) {													
					$params = substr($request_url, $i+strlen($action_name), strlen($request_url) - $i);
					$i = strpos($request_url, $this->sess_lang);
					if ($i === false) {
						$wanted_url = '/'.$module_name.'/'.$action_name.'/'.$this->sess_lang.$params;
						$this->redirect($wanted_url);
					}
				} else {
					// 模块首页
					$wanted_url = '/'.$module_name.'/'.$this->sess_lang;
					if ($wanted_url != $request_url)				
						$this->redirect($wanted_url);
				}
			} else {
				// 首页
				$wanted_url = '/'.$this->sess_lang;
				//var_dump($wanted_url.'|'.$request_url);
				if ($wanted_url != $request_url)				
					$this->redirect($wanted_url);
			}			
		} else {
			$module_name = $this->getModuleName();
			$action_name = $this->getActionName();
			$request_url = parse_url($request->getUri());
			$request_url = $request_url['path'];
			$i = strpos($request_url, $module_name);			
			if ($i !== false) {
				$i = strpos($request_url, $action_name);				
				if ($i !== false) {
					$params = substr($request_url, $i+strlen($action_name), strlen($request_url) - $i);
					$tmp_lang = substr($params, 1, 2);
					if (in_array($tmp_lang, array('en'))) {
						$params = substr($params, 4);
						$wanted_url = '/'.$module_name.'/'.$action_name.'/'.$params;					
						$this->redirect($wanted_url);
					}																	
				} else {
					// 模块首页
					$wanted_url = '/'.$module_name;
					if ($wanted_url != $request_url)				
						$this->redirect($wanted_url);						
				}
			} else {
				// 首页
				$wanted_url = '/';
				//var_dump($wanted_url.'|'.$request_url);
				if ($wanted_url != $request_url)				
					$this->redirect($wanted_url);
			}
		}
	}
}

class ajaxAction extends sfAction 
{
	public function execute($request) {
		$isAjax = $request->isXmlHttpRequest();
		if (!$isAjax) {	
			if ($request->getReferer())		
				$this->redirect($request->getReferer());
			else
				$this->redirect('/');
			return sfView::NONE;
		}
	}
}